/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.*;

/**
 *
 * @author LaiHuynh
 */
public class ChiTietHoaDonBanHang extends javax.swing.JInternalFrame {

    private DefaultTableModel dtm;
    private int maHoaDon;
    private int maKhachHang;
    private int maCuaHang;
    private Date d;

    public Date getD() {
        return d;
    }

    public void setD(Date d) {
        this.d = d;
    }

    public int getMaCuaHang() {
        return maCuaHang;
    }

    public void setMaCuaHang(int maCuaHang) {
        this.maCuaHang = maCuaHang;
    }

    public int getMaHoaDon() {
        return maHoaDon;
    }

    public void setMaHoaDon(int maHoaDon) {
        this.maHoaDon = maHoaDon;
    }

    public int getMaKhachHang() {
        return maKhachHang;
    }

    public void setMaKhachHang(int maKhachHang) {
        this.maKhachHang = maKhachHang;
    }

    /**
     * Creates new form ChiTietHoaDonBanHang
     */
    public ChiTietHoaDonBanHang() {
        initComponents();
    }

    private void hienThiDanhSach() {
        //doc du lieu
        this.dtm = new DefaultTableModel();
        List<ChiTietHoaDon> ds = ChiTietHoaDonDAO.layDanhSachChiTietHoaDon();
        this.dtm.addColumn("Mã Hóa Đơn");
        this.dtm.addColumn("Mã Sản Phẩm");
        this.dtm.addColumn("Số Lượng");
        for (int i = 0; i < ds.size(); i++) {
            ChiTietHoaDon cthd = ds.get(i);
            Vector vt = new Vector();
            HoaDon hd = cthd.getHoaDon();
            vt.add(hd.getMaHoaDon());
            SanPham sp = cthd.getSanPham();
            vt.add(sp.getMaSanPham());
            vt.add(cthd.getSoLuongMua());
            dtm.addRow(vt);
        }
        this.jtDanhSach.setModel(dtm);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtDanhSach = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jbtBoXung = new javax.swing.JButton();
        jbtDong = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jtfMaHoaDon = new javax.swing.JTextField();
        jtfMaKhachHang = new javax.swing.JTextField();
        jtfMaSanPham = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jtfDonGia = new javax.swing.JTextField();
        jtfSoLuong = new javax.swing.JTextField();
        jtfThanhTien = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jtfKhuyenMai = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtaThongTinKhuyenMai = new javax.swing.JTextArea();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Chi tiết hóa đơn bán hàng");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 255));
        jLabel1.setText("BỔ SUNG CHI TIẾT");

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách các hóa đơn"));

        jtDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtDanhSach);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Các chức năng"));

        jbtBoXung.setText("Bổ xung");
        jbtBoXung.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtBoXungActionPerformed(evt);
            }
        });

        jbtDong.setText("Đóng");
        jbtDong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtDongActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbtBoXung)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbtDong, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtBoXung)
                    .addComponent(jbtDong))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin bổ sung"));

        jLabel2.setText("Mã hóa đơn:");

        jLabel3.setText("Mã khách hàng:");

        jLabel4.setText("Số lượng:");

        jtfMaHoaDon.setEnabled(false);

        jtfMaKhachHang.setEnabled(false);

        jtfMaSanPham.setEnabled(false);

        jButton1.setText(">");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jtfDonGia.setEnabled(false);

        jtfSoLuong.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jtfSoLuongCaretUpdate(evt);
            }
        });
        jtfSoLuong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfSoLuongActionPerformed(evt);
            }
        });

        jtfThanhTien.setEnabled(false);

        jLabel5.setText("Mã sản phẩm:");

        jLabel6.setText("Đơn giá:");

        jLabel7.setText("Thành tiền:");

        jLabel8.setText("Khuyến mãi");

        jtfKhuyenMai.setText("0");

        jLabel9.setText("%");
        jLabel9.setToolTipText("");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel8)
                    .addComponent(jLabel4)
                    .addComponent(jLabel7))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jtfDonGia, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                                .addComponent(jtfThanhTien)
                                .addComponent(jtfSoLuong))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jtfKhuyenMai, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel9))
                            .addComponent(jtfMaKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfMaHoaDon, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jtfMaSanPham)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)
                        .addGap(19, 19, 19))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtfMaHoaDon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfMaKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfMaSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfKhuyenMai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jtfThanhTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin khuyến mãi"));

        jtaThongTinKhuyenMai.setColumns(20);
        jtaThongTinKhuyenMai.setRows(5);
        jtaThongTinKhuyenMai.setEnabled(false);
        jScrollPane2.setViewportView(jtaThongTinKhuyenMai);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(155, 155, 155))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(15, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 2, Short.MAX_VALUE)))
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        TimKiemSanPham_Dialog dialog = new TimKiemSanPham_Dialog(null, true);
        dialog.setVisible(true);
        int maSanPham = dialog.getMaSanPham();
        double donGia = dialog.getGiaBan();
        this.jtfMaSanPham.setText(String.valueOf(maSanPham));
        this.jtfDonGia.setText(String.valueOf(donGia));
        int kq = KtraKM(maSanPham, d);
        String thongbao = "";
        if (kq == 0) {
            thongbao = "không có khuyến mãi";
        } else {
            thongbao = "Khuyên mãi: " + kq;
        }
        jtaThongTinKhuyenMai.setText(thongbao);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        jtfMaHoaDon.setText(String.valueOf(this.getMaHoaDon()));
        jtfMaKhachHang.setText(String.valueOf(this.getMaKhachHang()));
        this.hienThiDanhSach();
    }//GEN-LAST:event_formInternalFrameOpened

    private void jtfSoLuongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfSoLuongActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfSoLuongActionPerformed

    private void jtfSoLuongCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jtfSoLuongCaretUpdate
        // TODO add your handling code here:
        double donGia = Double.parseDouble(this.jtfDonGia.getText());
        int soLuong = Integer.parseInt(this.jtfSoLuong.getText());
        double khuyenMai = Double.parseDouble(this.jtfKhuyenMai.getText());
        double thanhTien = donGia * soLuong * ((100- khuyenMai) / 100);
        DecimalFormat df = new DecimalFormat("0.0");
        String str = df.format(thanhTien);
        this.jtfThanhTien.setText(String.valueOf(str));
    }//GEN-LAST:event_jtfSoLuongCaretUpdate

    private void jbtDongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtDongActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jbtDongActionPerformed

    private void jbtBoXungActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtBoXungActionPerformed
        // TODO add your handling code here:
        try {
            int maHoaDon = Integer.parseInt(jtfMaHoaDon.getText());
            int maSanPham = Integer.parseInt(jtfMaSanPham.getText());
            int soLuong = Integer.parseInt(jtfSoLuong.getText());
            //them
            QuyDinh qd = QuiDinhDAO.layAllQuyDinh(maCuaHang);
            int noMax = qd.getNoKhongQua();
            int luongTonSauKhiBan = qd.getLuongTonSauKhiBan();
            KhachHang kh = KhachHangDAO.layThongTinKhachHang(maKhachHang);
            double tienNo = kh.getTienNo();
            if (tienNo > noMax) {
                JOptionPane.showMessageDialog(this, "Tiền nợ quý khách đã quá quy định để mua sản phẩm");
                return;
            }
            SanPham sp = SanPhamDAO.layThongTinSanPham(maSanPham);
            int luongTon = sp.getSoLuongTon();
            double donGia = Double.parseDouble(this.jtfDonGia.getText());
            double tongTien = tienNo + (soLuong * donGia);
            if ((luongTon - soLuong) < luongTonSauKhiBan) {
                JOptionPane.showMessageDialog(this, "Số lượng tồn của sản phẩm này sau khi bán đã nhỏ hơn quy định");
                return;
            }
            int soLuongTonNew = luongTon - soLuong;
            sp.setSoLuongTon(soLuongTonNew);
            SanPhamDAO.capNhatThongTinSanPham(sp);
            kh.setTienNo(tongTien);
            KhachHangDAO.capNhatThongTinKhachHang(kh);
            BaoCaoTon bct = new BaoCaoTon();
            bct.setSanPham(sp);
            bct.setNgayPhatSinh(d);
            bct.setTonDau(luongTon);
            bct.setPhatSinh(soLuongTonNew - luongTon);
            bct.setTonCuoi(soLuongTonNew);
            BaoCaoTonDAO.themThongBaoCaoTon(bct);

            BaoCaoCongNo cn = new BaoCaoCongNo();
            cn.setKhachHang(kh);
            cn.setNgayPhatSinh(d);
            cn.setNoDau(tienNo);
            cn.setNoCuoi(tongTien);
            cn.setPhatSinh(tongTien - tienNo);
            BaoCaoCongNoDAO.themThongBaoCaoCongNo(cn);
            //end them
            ChiTietHoaDon cthd = new ChiTietHoaDon();
            ChiTietHoaDonId id = new ChiTietHoaDonId();
            id.setMaHoaDon(maHoaDon);
            id.setMaSanPham(maSanPham);
            cthd.setId(id);
            cthd.setSoLuongMua(soLuong);
            ChiTietHoaDonDAO.themChiTietHoaDon(cthd);
            JOptionPane.showMessageDialog(this, "Thêm Thành Công");
            this.hienThiDanhSach();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Sản phẩm đã có trong hóa đơn");
        }
    }//GEN-LAST:event_jbtBoXungActionPerformed
    public static int KtraKM(int masp, Date now) {
        int kq = 1;
        ChuongTrinhKhuyenMai km = null;
        km = SanPhamKMDAO.LayMa(masp);
        if (km == null) {
            kq = 0;
            //return kq;
        } else {

            Date bd = km.getNgayBatDau();
            Date kt = km.getNgayKetThuc();
            if (bd.compareTo(now) <= 0 && kt.compareTo(now) >= 0) {
                kq = Integer.valueOf(km.getKhuyenMai());
            }
        }
        return kq;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtBoXung;
    private javax.swing.JButton jbtDong;
    private javax.swing.JTable jtDanhSach;
    private javax.swing.JTextArea jtaThongTinKhuyenMai;
    private javax.swing.JTextField jtfDonGia;
    private javax.swing.JTextField jtfKhuyenMai;
    private javax.swing.JTextField jtfMaHoaDon;
    private javax.swing.JTextField jtfMaKhachHang;
    private javax.swing.JTextField jtfMaSanPham;
    private javax.swing.JTextField jtfSoLuong;
    private javax.swing.JTextField jtfThanhTien;
    // End of variables declaration//GEN-END:variables
}
